package xxh;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;

public class BackgroundPanel extends JPanel {
    private Image backgroundImage;

    // 构造函数加载背景图
    public BackgroundPanel(String imagePath, int width, int height) {
        try {
            backgroundImage = ImageIO.read(new File(imagePath));
            setPreferredSize(new Dimension(width, height));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 重载paintComponent方法，绘制背景图
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);  // 绘制背景图片
        }
    }
}
